<%# The license inside this block applies to this file.
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
# frozen_string_literal: false

<%= lines(autogen_notice(:ruby, pwd)) -%>
<%
  require pwd + '/google/string_utils'


  inside_indent = 8

  requires = generate_requires(object.all_user_properties)
  requires << 'gcp_backend'
-%>
<%= lines(emit_requires(requires)) -%>

# A provider to manage <%= @api.display_name -%> resources.
class <%= object.__product.name.camelize(:upper) -%><%= object.name -%> < GcpResourceBase
  name '<%= resource_name(object, product) -%>'
  desc '<%= object.name -%>'
  supports platform: 'gcp'

  attr_reader :params
<% object.all_user_properties.each do |prop| -%>
  <%= "attr_reader :#{prop.out_name}" -%>

<% end -%>

<% if !object.singular_custom_constructor.nil? -%>
<%= indent(compile(pwd + '/' + object.singular_custom_constructor), 2) -%>

<% elsif object.nested_query.nil? -%>
  def initialize(params)
    super(params.merge({ use_http_transport: true }))
    @params = params
    @fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
    parse unless @fetched.nil?
  end
<% else # object.nested_query.nil? -%>
  def initialize(params)
    super(params.merge({ use_http_transport: true }))
    @params = params
    fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
    @fetched = unwrap(fetched, params)
    parse unless @fetched.nil?
  end

<% identity_outs = object.identity.map { |id| id.out_name } -%>
  def identity
    %w{<%= identity_outs.join(' ') -%>}
  end

  def collection_item
    '<%= object.collection_url_key -%>'
  end

  def unwrap(fetched, params)
    fetched[collection_item].find { |result| identity.all? { |id| result[id] == params[id.to_sym] } }
  end
<% end # object.nested_query.nil? -%>

  def parse
<%
  parse_code = object.all_user_properties.map do |prop|
    assignment = "@#{prop.out_name} = #{parse_code(prop, '@fetched')}"
  end
-%>
<%= lines(indent(parse_code, 4)) -%>
  end
<% if time_prop?(object.all_user_properties) -%>

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end
<% end # has_time_prop -%>

  def exists?
    !@fetched.nil?
  end

<% 
individual_url = format_url(object.self_link_url)
best_guess_identifier = extract_identifiers(individual_url).last 
-%>
  def to_s
    "<%= object.name -%> #{@params[:<%= best_guess_identifier -%>]}"
  end
<% unless object&.additional_functions.nil? -%>

<%= lines(indent(compile(pwd + '/' + object.additional_functions), 2)) -%>
<% end -%>

  private

<%= compile(pwd + '/templates/inspec/product_url.erb') -%>

<% url = object.self_link || object.base_url + '/{{name}}' -%>
<% url_params = extract_identifiers(individual_url) -%>
<%
url_params.each do |p|
  url = url.gsub("{{#{p}}}", "{{#{p.underscore}}}")
end
-%>
  def resource_base_url
    '<%= url %>'
  end
end
